
package net.atremmedia.gwt.actionjava.examples.frame;

import net.atremmedia.gwt.actionjava.examples.AJExample;
import actionjava.events.tick.TickEvent;
import actionjava.session.view.BaseView2D;
import actionjava.session.view.CanvasInUseException;
import actionjava.session.view.CanvasNotFoundException;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.EntryPoint;

public abstract class AJExampleOverview extends BaseView2D implements EntryPoint
{
	private double width = 800;
	private double height = 600;
	private AJExample curExample = null;
	private AJExampleBackButton backButton = null;
	private AJExampleRefreshButton refreshButton = null;
	private AJExampleSourceButton srcButton = null;
	private AJExample[] examples = null;
	private AJExampleButton[] buttons = null;
	private AJExampleTitle titleField;

	public AJExampleOverview(String canvasID, double width, double height) throws CanvasNotFoundException, CanvasInUseException
	{
		super(canvasID);
		this.width = width;
		this.height = height;
	}

	//
	//
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// - - Public
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	//
	//

	public void draw()
	{
		buttons = new AJExampleButton[examples.length];
		for(int i = 0;i < examples.length;i++) {
			AJExample example = examples[i];
			buttons[i] = new AJExampleButton(10, (10 + (i * 20)), (i + 1), example, this);
		}
	}

	public void destroy()
	{
		for(int i = 0;i < buttons.length;i++) {
			AJExampleButton button = buttons[i];
			button.destroy();
		}
	}

	public void loadExample(AJExample example)
	{
		Log.debug("LOADING: " + example.getName());

		destroy();
		addButtons(example.getName());
		curExample = example;
		curExample.run();
	}

	public void refreshExample()
	{
		Log.debug("REFRESH: " + curExample.getName());

		removeButtons();
		curExample.destroy();
		addButtons(curExample.getName());
		curExample.run();
	}

	public void unloadExample()
	{
		Log.debug("LOADING: Overview");
		setBGColor("#FFFFFF");

		removeButtons();
		curExample.destroy();
		curExample = null;
		draw();
	}

	public void addButtons(String title)
	{
		backButton = new AJExampleBackButton(10, 565, this);
		refreshButton = new AJExampleRefreshButton(45, 565, this);
		srcButton = new AJExampleSourceButton(80, 565, this);
		titleField = new AJExampleTitle(115, 565, title, this);
	}

	public void removeButtons()
	{
		backButton.destroy();
		refreshButton.destroy();
		srcButton.destroy();
		titleField.destroy();
	}

	//
	//
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// - - Abstracts
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	//
	//

	@Override
	public void run()
	{
		draw();
	}

	@Override
	public void onRenderTick(TickEvent event)
	{
		if(curExample != null) {
			curExample.onRenderTick(event);
		}
	}

	//
	//
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	// - - GET / SET
	// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
	//
	//

	public void setBGColor(String color)
	{
		getCanvas().getStyle().setBackgroundColor(color);
	}

	public double getWidth()
	{
		return width;
	}

	public void setWidth(double width)
	{
		this.width = width;
	}

	public double getHeight()
	{
		return height;
	}

	public void setHeight(double height)
	{
		this.height = height;
	}

	public AJExample[] getExamples()
	{
		return examples;
	}

	public void setExamples(AJExample[] examples)
	{
		this.examples = examples;
	}

	public AJExample getCurExample()
	{
		return curExample;
	}

	public void setCurExample(AJExample curExample)
	{
		this.curExample = curExample;
	}
}
